import {Component, Input, OnInit, ViewChild} from "@angular/core";
import {LocalDataSource} from "ng2-smart-table";
import {SmartTableData} from "../../@core/data/smart-table";
import {ActivatedRoute} from "@angular/router";
import {NbDialogService} from "@nebular/theme";
import {AdminService} from "../../AdminService";
import {DevService} from "../../energy/api";
import {BaseRequest} from "../../utils/BaseRequest";
import {map} from "rxjs/operators";
import {ResponseDeviceInfo} from "../../energy/api/model/responseDeviceInfo";
import {DeviceLinkRender} from "../device-document/device-document.component";

@Component({
  selector:'device-ctrl-component',
  templateUrl: './device-ctrl.component.html',
  styles: [
      `
      nb-card {
        transform: translate3d(0, 0, 0);
      }

      button {
        margin-top: 5px;
        margin-bottom: 5px;
      }
      .margin {
        margin: 10px;
      }
    `
  ]
})
export class DeviceCtrlComponent implements OnInit{
  @Input("unitId") unitId;
  @Input("showFilter") showFilter?:boolean;
  settings = {
    pager:{
      display:true,
      perPage:20
    },
    hideSubHeader: true,
    actions: {
      add: false,
      edit: false,
      delete: false,
      columnTitle: '操作',
      position: 'right',
      custom:[
        {
          name:'ctrl',
          title: '<a href="javascript:void(0)" style="font-size:medium !important;">控制</a>',
        },
      ]
    },
    columns: {
      devNo: {
        title: '编号',
        type: 'custom',
        renderComponent: DeviceLinkRender
      },
      devName: {
        title: '设备名称',
        type: 'string',
      },
      devType: {
        title: '设备类型',
        type: 'string',
      },
      usesType: {
        title: '使用类型',
        type: 'string',
      },
      enableDate: {
        title: '启用日期',
        type: 'string',
      },
      devStatus: {
        title: '设备状态',
        type: 'string',
      },
      runStatus: {
        title: '运行状态',
        type: 'string',
      },
      devIp: {
        title: '设备IP',
        type: 'string',
      },
      devMac: {
        title: '设备MAC',
        type: 'string',
      },
      extInfo: {
        title: '额外信息',
        type: 'string',
      },
    },
  };
  source: DataDataSource;
  maxPage:number = 1;
  page:number = 1;
  limit:number = 25;
  ud?:number=null;
  devNo:string = '';
  devName: string = '';
  devType: string = '';
  @ViewChild("dialog") devCtrlDialog;
  constructor(private service: SmartTableData
              ,private api:DevService
              ,private route: ActivatedRoute
              ,private dialog:NbDialogService
              ,private admin:AdminService) {
  }

  ngOnInit(): void {
    if(this.unitId==null) {
      this.route.paramMap.subscribe(value => {
        const ud = value.get("unitId");
        if (ud != null) this.ud = Number(ud);
        console.log(this.ud);
        this.source = new DataDataSource(this.api, this.ud);
      });
    }else{
      this.ud = Number(this.unitId);
      this.source = new DataDataSource(this.api, this.ud);
    }
  }

  filter(){
    this.source.devNo = this.devNo;
    this.source.devName = this.devName;
    this.source.devType = this.devType;
    this.source.setPage(1);
  }


  onCtrl(event){
    this.dialog.open(this.devCtrlDialog).onClose.subscribe(v=>{
      if(v!=null)
        alert("发送失败，设备尚未连接")
    })
  }
}

export class DataDataSource extends LocalDataSource {
  devNo:string = '';
  devName: string = '';
  devType: string = '';
  all: number = -1;
  constructor(private api: DevService,private unitId?:number) {
    super()
  }

  count(): number {
    return this.all;
  }

  getElements(): Promise<any> {
    const query = {
      pageIndex: (this.pagingConf['page'] - 1) * this.pagingConf['perPage'],
      pageSize: this.pagingConf['perPage'],
      unitId: this.unitId
    };
    if(this.devNo!='') query['devNo'] = this.devNo;
    if(this.devName!='') query['devName'] = this.devName;
    if(this.devType!='') query['devType'] = this.devType;
    return this.api.postECAGetDeviceInfo(new BaseRequest(query)).pipe(map((res:ResponseDeviceInfo)=>{
      this.all = res.dataCount;
      return res.data;
    })).toPromise();
  }
}

